package com.wanban.videoadutils

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ProgressBar
import android.widget.RelativeLayout
import com.wb.adutils.AdBaseLoader
import com.wb.adutils.AdLoader
import com.wb.adutils.AdUtils
import com.wb.adutils.callback.OnLoadAdListener
import com.wb.adutils.db.AdEntity
import com.wb.adutils.tools.AdConstants
import kotlin.collections.ArrayList

class MainActivity : AppCompatActivity() {

    private var bannerAdLoader: AdBaseLoader? = null
    private var nativeAdLoader: AdBaseLoader? = null
    private var interstitialAdLoader: AdBaseLoader? = null
    private var rewardAdLoader: AdBaseLoader? = null
    private var openAdLoader: AdBaseLoader? = null

    private lateinit var bannerBtn: Button
    private lateinit var bannerLayout: ViewGroup
    private lateinit var bannerRoot: RelativeLayout

    private lateinit var nativeBtn: Button
    private lateinit var nativeRoot: ViewGroup

    private lateinit var interstitialBtn: Button
    private lateinit var showInterstitialBtn: Button
    private lateinit var pbInterstitial: ProgressBar
    private lateinit var pbOpen: ProgressBar
    private lateinit var rewardBtn: Button
    private lateinit var showRewardBtn: Button
    private lateinit var pbReward: ProgressBar
    private lateinit var openBtn: Button
    private lateinit var openRoot: RelativeLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
        bindEvent()
        initData()
    }

    private fun initView(){
        bannerBtn = findViewById(R.id.btnBanner)
        bannerLayout = findViewById(R.id.bannerLayout)
        bannerRoot = findViewById(R.id.bannerRoot)
        nativeBtn = findViewById(R.id.nativeBtn)
        interstitialBtn = findViewById(R.id.interstitialBtn)
        showInterstitialBtn = findViewById(R.id.showInterstitialBtn)
        pbInterstitial = findViewById(R.id.pbInterstitial)
        rewardBtn = findViewById(R.id.rewardBtn)
        showRewardBtn = findViewById(R.id.showRewardBtn)
        pbReward = findViewById(R.id.pbReward)
        openBtn = findViewById(R.id.openBtn)
        nativeRoot = findViewById(R.id.nativeRoot)
        openRoot = findViewById(R.id.openRoot)
        pbOpen = findViewById(R.id.pbOpen)
    }

    private fun bindEvent(){
        bannerBtn.setOnClickListener {
            bannerAdLoader?.release()
            bannerAdLoader = AdLoader.loadAd(this, AdConstants.AD_TEMPLATE_EXIT, width = 300, height = 150, callback =  object: OnLoadAdListener {
                override fun onAdLoaded(ad: Any?) {
                    if (isFinishing || isDestroyed) return
                    bannerLayout.visibility = View.VISIBLE
                    bannerAdLoader?.showAd(bannerRoot)
                }

                override fun onAdLoadFail(errorCode: Int) {
                    if (isFinishing || isDestroyed) return
                    bannerLayout.visibility = View.GONE
                    bannerRoot.removeAllViews()
                }

                override fun onAdComplete(isRewarded: Boolean) {
                    if (isFinishing || isDestroyed) return
                    bannerLayout.visibility = View.GONE
                    bannerRoot.removeAllViews()
                }
            })
        }

        nativeBtn.setOnClickListener {
            nativeAdLoader?.release()
            nativeAdLoader = AdLoader.loadAd(this, AdConstants.AD_TEMPLATE_VIDEO_BANNER, width = 360, height = 201, callback = object: OnLoadAdListener {
                override fun onAdLoaded(ad: Any?) {
                    if (isFinishing || isDestroyed) return
                    nativeAdLoader?.showAd(nativeRoot)
                }

                override fun onAdLoadFail(errorCode: Int) {
                    if (isFinishing || isDestroyed) return
                    nativeRoot.removeAllViews()
                }

                override fun onAdComplete(isRewarded: Boolean) {
                    if (isFinishing || isDestroyed) return
                    nativeRoot.removeAllViews()
                }
            })
        }

        interstitialBtn.setOnClickListener {
            pbInterstitial.visibility = View.VISIBLE
            showInterstitialBtn.visibility = View.GONE

            interstitialAdLoader?.release()
            interstitialAdLoader = AdLoader.loadAd(this, AdConstants.AD_TEMPLATE_VIDEO, object: OnLoadAdListener {
                override fun onAdLoaded(ad: Any?) {
                    if (isFinishing || isDestroyed) return
                    pbInterstitial.visibility = View.GONE
                    showInterstitialBtn.visibility = View.VISIBLE
                }

                  override fun onAdLoadFail(errorCode: Int) {
                    if (isFinishing || isDestroyed) return
                    pbInterstitial.visibility = View.GONE
                    showInterstitialBtn.visibility = View.GONE
                }

                override fun onAdComplete(isRewarded: Boolean) {
                    if (isFinishing || isDestroyed) return
                    pbInterstitial.visibility = View.GONE
                    showInterstitialBtn.visibility = View.GONE
                }
            })
        }
        showInterstitialBtn.setOnClickListener {
            interstitialAdLoader?.showAd(bannerRoot)
            showInterstitialBtn.visibility = View.GONE
        }

        rewardBtn.setOnClickListener {
            pbReward.visibility = View.VISIBLE
            showRewardBtn.visibility = View.GONE

            rewardAdLoader?.release()
            rewardAdLoader = AdLoader.loadAd(this, AdConstants.AD_TEMPLATE_DOWNLOAD, object: OnLoadAdListener {
                override fun onAdLoaded(ad: Any?) {
                    if (isFinishing || isDestroyed) return
                    pbReward.visibility = View.GONE
                    showRewardBtn.visibility = View.VISIBLE
                }

                  override fun onAdLoadFail(errorCode: Int) {
                    if (isFinishing || isDestroyed) return
                    pbReward.visibility = View.GONE
                    showRewardBtn.visibility = View.GONE
                }

                override fun onAdComplete(isRewarded: Boolean) {
                    if (isFinishing || isDestroyed) return
                    pbReward.visibility = View.GONE
                    showRewardBtn.visibility = View.GONE
                }
            })
        }
        showRewardBtn.setOnClickListener {
            rewardAdLoader?.showAd(bannerRoot)
            showRewardBtn.visibility = View.GONE
        }

        openBtn.setOnClickListener {
            pbOpen.visibility = View.VISIBLE
            openAdLoader?.release()
            openAdLoader = AdLoader.loadAd(this, AdConstants.AD_TEMPLATE_SPLASH, width = 1080, height = 1920, callback =  object: OnLoadAdListener {
                override fun onAdLoaded(ad: Any?) {
                    if (isFinishing || isDestroyed) return
                    pbOpen.visibility = View.GONE
                    openAdLoader?.showAd(openRoot)
                }

                  override fun onAdLoadFail(errorCode: Int) {
                    if (isFinishing || isDestroyed) return
                    pbOpen.visibility = View.GONE
                    openRoot.removeAllViews()
                }

                override fun onAdComplete(isRewarded: Boolean) {
                    pbOpen.visibility = View.GONE
                    openRoot.visibility = View.GONE
                    openRoot.removeAllViews()
                }
            })
        }
    }


    private fun initData(){
        val ads = getAdList()
        AdUtils.insertAdList(ads)
    }

    override fun onResume() {
        super.onResume()
        bannerAdLoader?.onResume()
        nativeAdLoader?.onResume()
    }

    override fun onPause() {
        super.onPause()
        bannerAdLoader?.onPause()
        nativeAdLoader?.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        bannerAdLoader?.release()
        nativeAdLoader?.release()
        interstitialAdLoader?.release()
        rewardAdLoader?.release()
        openAdLoader?.release()
    }


    private fun getAdList(): ArrayList<AdEntity> {
        val ads = ArrayList<AdEntity>()
        // banner
        val bannerAd = AdEntity()
        bannerAd.adTemplate = AdConstants.AD_TEMPLATE_EXIT
        bannerAd.adUnit = "102110609"
        bannerAd.adInterval = 0
        bannerAd.adType = 0
        bannerAd.clearCache = 0
        bannerAd.downloadDefaultCount = 10
        bannerAd.downloadAddCount = 2
        bannerAd.timeout = 7
        bannerAd.txt = ""
        ads.add(bannerAd)
        // 信息流
        val native = AdEntity()
        native.adTemplate = AdConstants.AD_TEMPLATE_VIDEO_BANNER
        native.adUnit = "102111101"
        native.adInterval = 0
        native.adType = 0
        native.clearCache = 0
        native.downloadDefaultCount = 10
        native.downloadAddCount = 2
        native.timeout = 7
        native.txt = ""
        ads.add(native)
        // 插屏
        val interstitial = AdEntity()
        interstitial.adTemplate = AdConstants.AD_TEMPLATE_VIDEO
        interstitial.adUnit = "102110851"
        // 广告请求间隔，以秒为单位
        interstitial.adInterval = 15
        // 广告类型，主要是给详情广告使用，有插屏和激励  插屏0：激励：1
        interstitial.adType = 0
        interstitial.clearCache = 0
        interstitial.downloadDefaultCount = 10
        interstitial.downloadAddCount = 2
        interstitial.timeout = 7
        interstitial.txt = "详情页广告 60s加载一次"
        ads.add(interstitial)
        // 激励
        val reward = AdEntity()
        reward.adTemplate = AdConstants.AD_TEMPLATE_DOWNLOAD
        reward.adUnit = "102110907"
        // 广告请求间隔，以秒为单位
        reward.adInterval = 60
        // 广告类型，主要是给详情广告使用，有插屏和激励  插屏0：激励：1
        reward.adType = 0
        reward.clearCache = 0
        reward.downloadDefaultCount = 10
        reward.downloadAddCount = 2
        reward.timeout = 7
        reward.txt = "详情页广告 60s加载一次"
        ads.add(reward)

        // 开屏
        val open = AdEntity()
        open.adTemplate = AdConstants.AD_TEMPLATE_SPLASH
        open.adUnit = "102110195"
        // 广告请求间隔，以秒为单位
        open.adInterval = 0
        // 广告类型，主要是给详情广告使用，有插屏和激励  插屏0：激励：1
        open.adType = 0
        open.clearCache = 0
        open.downloadDefaultCount = 10
        open.downloadAddCount = 2
        open.timeout = 7
        open.txt = "详情页广告 60s加载一次"
        ads.add(open)
        return ads
    }
}